package com.ruoyi.system.common;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ContextHolderUtils {

	public static ServletContext servletContext;
	/**
	 * 获取ServletContext实例
	 * @return
	 */
	public static ServletContext getInstance(){
		if(servletContext==null){
			servletContext = getServletContext();
		}
		return servletContext;
	}
	/**
	 * SpringMvc下获取request
	 *
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	public static HttpServletResponse getResponse() {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
		return response;
	}

	/**
	 * SpringMvc下获取session
	 *
	 * @return
	 */
	public static HttpSession getSession() {
		HttpSession session = getRequest().getSession();
		return session;

	}

	/**
	 * springmvc获取ServletContext
	 * @return
	 */
	private static ServletContext getServletContext(){
		return getSession().getServletContext();
	}


}
